using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using AdHocReports.Properties;
using System.Text;

namespace AdHocReports
{
	/// <summary>
	/// Summary description for error.
	/// </summary>
	public class error : System.Web.UI.Page
	{
		protected System.Web.UI.HtmlControls.HtmlInputButton cmdBack;
		protected System.Web.UI.HtmlControls.HtmlInputButton cmdNext;
		protected System.Web.UI.WebControls.Label lblMessage;
		protected System.Web.UI.WebControls.Button cmdSend;
		protected System.Web.UI.HtmlControls.HtmlInputHidden shMessage;
		protected System.Web.UI.WebControls.Label lblSend;
		protected System.Web.UI.HtmlControls.HtmlInputButton cmdCancel;
        protected System.Web.UI.WebControls.TextBox errorMessage;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
            if ((Context.AllErrors != null) && (Context.AllErrors.Length == 0) && (errorMessage.Text.Length == 0))
            {
                Response.Redirect("~/");
            }
            else
            {
                if (errorMessage.Text.Length == 0)
                {
                    errorMessage.Text = GetErrorMessage();
                }
            }
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.cmdNext.ServerClick += new System.EventHandler(this.cmdNext_ServerClick);
			this.cmdSend.Click += new System.EventHandler(this.cmdSend_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void cmdNext_ServerClick(object sender, System.EventArgs e)
		{
			Session.Remove("sConnectionString");
			Response.Redirect("~/");
		}

		private void cmdSend_Click(object sender, System.EventArgs e)
		{
            if (Global.SendMail(Settings.Default.ErrorFrom, Settings.Default.ErrorTo, Settings.Default.ErrorCC, Settings.Default.ErrorBcc, Settings.Default.ErrorSubject, errorMessage.Text) == false)
			{
				lblMessage.Text = "There was a problem sending the email. Please contact the vendor by phone.";
				lblSend.Visible = false;
				cmdSend.Visible = false;
			}

            Context.Items.Remove("ExceptionSessionKeys");

            Response.Redirect("~/");
		}

        private string GetErrorMessage()
        {
            StringBuilder result = new StringBuilder();

            foreach (Exception ex in Context.AllErrors)
            {
                result.Append(ex.ToString());
                result.Append(Environment.NewLine);
                result.Append(Environment.NewLine);
            }

            if (Context.Items["ExceptionSessionKeys"] != null)
            {
                result.Append(Context.Items["ExceptionSessionKeys"].ToString());
            }

            return result.ToString();
        }
	}
}
